package com.iyang.bootadmin.serivcie.impl;

import com.iyang.bootadmin.mapper.MenuMapper;
import com.iyang.bootadmin.mapper.UserMapper;
import com.iyang.bootadmin.pojo.Menu;
import com.iyang.bootadmin.pojo.User;
import com.iyang.bootadmin.serivcie.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Mu_YI
 * @Date: 2019/3/9 14:43
 * @Version 1.0
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public boolean login(String username, String password) {
        return userMapper.login(username,password) != null;
    }

    @Override
    public Map<String,Object>  menuAll(){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            List<Menu> menuMapList = menuMapper.selectMenu();
            List<Menu> rootMenu = new ArrayList<>();
            for(Menu menu:menuMapList){
                if("0".equals(menu.getParentId())){
                    rootMenu.add(menu);
                }
            }
           if(rootMenu.size() > 1){
               Collections.sort(rootMenu,order());
           }
            for(Menu menu:rootMenu){
                // 获取根节点下的所有子节点
                List<Menu> childList = getChildMenu(menu.getMenuId(), menuMapList);
                menu.setChildren(childList);
            }
            resultMap.put("res",200);
            resultMap.put("entris",rootMenu);
            return resultMap;
        }catch (Exception e){
            resultMap.put("res",500);
            resultMap.put("entris",new ArrayList<Menu>());
            LOGGER.error(e.getMessage(),e);
            return resultMap;
        }
    }

    /**
     * 父级菜单进行排序
     * @return
     */
    private Comparator<Menu> order(){
        Comparator<Menu> comparator = new Comparator<Menu>() {
            @Override
            public int compare(Menu o1, Menu o2) {
                if(o1.getMenuOrder().equals(o2.getMenuOrder())){
                    return o1.getMenuOrder().intValue() - o2.getMenuOrder().intValue();
                }
                return 0;
            }
        };
        return comparator;
    }

    private List<Menu> getChildMenu(String id,List<Menu> allMenu){
        List<Menu> childList = new ArrayList<>();
        for(Menu menu:allMenu){
            if(menu.getParentId().equals(id)){
                childList.add(menu);
            }
        }
        // 递归
        for(Menu menu:childList){
            menu.setChildren(getChildMenu(menu.getMenuId(),allMenu));
        }
        if(childList.size() > 1){
            Collections.sort(childList,order());
        }
        if(childList.size() == 0){
            return new ArrayList<Menu>();
        }
        return childList;
    }


}
